<?php
    include 'include/function.php';
    valid_user();
    if (get_magic_quotes_gpc())
        $_GET['contest_id'] = stripslashes($_GET['contest_id']);
    $contest_id = mysql_escape_string($_GET['contest_id']);
    valid_contest($contest_id);
	include('include/header.php');
    $contest = get_contest_info($contest_id);
?>

<title>Contest</title>
    <div id="headline">
        <div class="wrapper">
            <h2>Contest - <?php echo $contest['title']; ?></h2>
        </div>
    </div>    

<div id="pagebody">
	<div class="wrapper">

    <div class="notice">
<?php
	echo '<strong>Start Time:</strong>' . $contest['start_time'] . '&nbsp;&nbsp;';
	echo '<strong>End Time:</strong>' . $contest['end_time'] . '<br>';
	$start = strtotime($contest['start_time']);
	$end = strtotime($contest['end_time']);
	$current = time();
	echo '<strong>Current Time:</strong><span id="tm">';
    echo date("Y-m-d H:i:s", $current);
    echo '</span>&nbsp;&nbsp;';
	echo '<strong>Status:';
	if ($current >= $end)
		echo '<font color=green>Finished</font>';
	else if ($current >= $start)
		echo "<font color=red>Running</font>";
	else
		echo "<font color=green>Started at:&nbsp;{$contest['start_time']}</font>";
    echo "</strong>";
?>
    
    </div>


    <table class="center" id="contest_list">
    <thead>
        <tr>
            <th width="50"></th>
            <th width="100">Problem Id</th>
            <th width="450">Title</th>
        </tr>
    </thead>
    <tbody>
<?php
if ($current >= $start) {
    $problem = get_problem_info($contest_id);
    $rows = count($problem);
    
    for ($i = 0; $i < $rows; $i++) {
        if ($i % 2 == 0)
            echo '<tr class=tro>';
        else
            echo '<tr class=tre>';
        echo '<td></td>';
        echo '<td>' . $problem[$i]['as_problem'] . '</td>';
        echo '<td><a href="' . $problem[$i]['url'] . '">' . $problem[$i]['title'] . '</a></td>';
    }
}
?>

    </tbody></table>

		
    <p class="center" style="padding-top: 14px;">
        <?php
            if ($current >= $start && $current < $end) {
                echo "<a class = \"link-button\" href=submit?contest_id=$contest_id>Submit</a>";
            }
            if ($current >= $start) {
                echo "<a class = \"link-button\"  href=standing?contest_id=$contest_id>Standing</a>";
                echo "<a class = \"link-button\"  href=status?contest_id=$contest_id>Status</a>";
            }
        ?>
    </p>
</div>
</div>

<?php
	include('include/footer.php');
?>

<script language="javascript">
var cstatus = <?php echo $current < $start ? 0 : 1; ?>;
var timestamp = <?php echo time(); ?> * 1000;
var starttime = <?php echo $start; ?> * 1000;
function update(){
    var y,m,d,g,i,s;
    timestamp+=1000;
    if (timestamp > starttime && cstatus == 0)
        window.location = window.location;
    var t = new Date(timestamp);
    y=t.getFullYear();
    m=t.getMonth()+1; if (m<10) m="0"+m;
    d=t.getDate(); if (d<10) d="0"+d;
    g=t.getHours(); if (g<10) g="0"+g;
    i=t.getMinutes(); if (i<10) i="0"+i;
    s=t.getSeconds(); if (s<10) s="0"+s;
    var str = y+"-"+m+"-"+d+" "+g+":"+i+":"+s;
    document.getElementById("tm").innerHTML = str;
}
setInterval("update()", 1000);
</script>
